package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallCollect;
import com.cskaoyan.bean.CskaoyanmallCollectExample;
import com.cskaoyan.mapper.CskaoyanmallCollectMapper;
import com.cskaoyan.service.CollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    CskaoyanmallCollectMapper cskaoyanmallCollectMapper;

    @Override
    public Map<String, Object> querySelective(Integer userId, Integer valueId, Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        CskaoyanmallCollectExample collectExample = new CskaoyanmallCollectExample();
        CskaoyanmallCollectExample.Criteria criteria = collectExample.createCriteria();

        if (!ObjectUtils.isEmpty(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        if (!ObjectUtils.isEmpty(valueId)){
            criteria.andValueIdEqualTo(valueId);
        }
        if (!ObjectUtils.isEmpty(sort)&&!ObjectUtils.isEmpty(order)){
            collectExample.setOrderByClause(sort+" "+order);
        }

        List<CskaoyanmallCollect> collects = cskaoyanmallCollectMapper.selectByExample(collectExample);
        PageInfo<CskaoyanmallCollect> collectPageInfo = new PageInfo<>(collects);
        long total = collectPageInfo.getTotal();
        HashMap<String, Object> res = new HashMap<>();
        res.put("items",collects);
        res.put("total",total);
        return res;
    }
}
